//  this library.
import 'package:flutter/material.dart';
import 'package:hktk_app/model/practice/practice_model.dart';
import 'package:hktk_app/util/font_util.dart';
import 'package:hktk_app/util/view_util.dart';
import 'package:hktk_app/widget/question/stem_rich_text_widget.dart';

/// 试卷详情内容展示
class PaperDetailWidget extends StatelessWidget {
  /// 模式，true：组卷详情，false：试卷详情
  final bool testType;

  /// 试卷名称显示
  final String? name;

  /// 试卷类型名称
  final String? typeName;

  /// 试卷总分数
  final int? totalScore;

  /// 总分数右边显示相关数据
  final int? scoreRight;

  /// 试题总数
  final int? qCount;

  /// 试题总数右边显示数据
  final int? qCountRight;

  /// 试卷模式内容显示
  final List<ContentPractice>? content;

  /// 组卷模式试题内容提示
  final String? remark;

  const PaperDetailWidget({
    super.key,
    required this.testType,
    this.name,
    this.typeName,
    this.totalScore,
    this.scoreRight,
    this.qCount,
    this.qCountRight,
    this.content,
    this.remark,
  });

  /// 试卷详情内容创建
  const PaperDetailWidget.paper({
    super.key,
    required this.name,
    this.typeName,
    this.totalScore,
    this.scoreRight,
    this.qCount,
    this.qCountRight,
    this.content,
  })  : testType = false,
        remark = null;

  /// 组卷详情内容创建
  const PaperDetailWidget.test({
    super.key,
    this.totalScore,
    this.scoreRight,
    this.qCount,
    this.remark,
  })  : testType = true,
        name = "模拟考试",
        typeName = null,
        qCountRight = null,
        content = null;

  @override
  Widget build(BuildContext context) {

    if (testType) {
      return Column(
        children: _listItem(),
      );
    } else {
      return ListView(
        children: _listItem(),
      );
    }
  }

  /// 内容数组显示
  List<Widget> _listItem() {
    return <Widget>[
      _titleItem(),
      hiSpace(height: 30),
      _typeItem(),
      hiSpace(height: 60),
      _scoreItem(),
      hiSpace(height: 54),
      _countItem(),
      hiSpace(height: 40),
      _lineItem(),
      hiSpace(height: 40),
      _questionTypeItem(),
    ];
  }

  /// 标题组件显示
  _titleItem() {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 20),
      child: Text(
        name ?? "",
        style: FontUtil.fontBlack1(30, fontWeight: FontWeight.w500),
        textAlign: TextAlign.center,
      ),
    );
  }

  /// 试卷类型显示
  _typeItem() {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 10),
      height: 40,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          _typeLineItem(),
          Text(
            typeName ?? "真题试卷",
            style: FontUtil.fontBlack1(20),
          ),
          _typeLineItem(),
        ],
      ),
    );
  }

  /// 试卷类型对应的横线
  _typeLineItem() {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Container(
          width: 40,
          height: 8,
          color: colorOrange11,
        ),
        hiSpace(height: 10),
        Container(
          width: 40,
          height: 2,
          color: colorOrange11,
        )
      ],
    );
  }

  /// 显示总分，时长
  _scoreItem() {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 10),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          _scoreNumItem(totalScore ?? 0, "考试总分"),
          Container(
            width: 2,
            height: 52,
            color: colorGrey7,
          ),
          _scoreNumItem(scoreRight ?? 0, testType ? "及格分" : "考试时长"),
        ],
      ),
    );
  }

  /// 分数具体显示
  _scoreNumItem(int num, String tips) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text.rich(
          TextSpan(
            children: [
              TextSpan(
                text: num.toString(),
                style: FontUtil.fontBlack1(32, fontWeight: FontWeight.w500),
              ),
              TextSpan(
                text: "分",
                style: FontUtil.fontBlack1(22),
              )
            ],
          ),
        ),
        Text(
          tips,
          style: FontUtil.fontGrey1(20),
        ),
      ],
    );
  }

  /// 试题数据组件
  _countItem() {
    return Container(
      margin: EdgeInsets.only(right: 20),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          // showDifficulty(num: _map["difficulty"]),
          Text(
            "总题数 ${qCount ?? 0}",
            style: FontUtil.fontGrey2(20),
          ),
          if (!testType)
            Text(
              "${qCountRight ?? 0}人刷过",
              style: FontUtil.fontGrey1(20),
            )
        ],
      ),
    );
  }

  /// 显示分割线
  _lineItem() {
    return Container(
      decoration: BoxDecoration(
        border: Border(bottom: BorderSide(color: colorGrey8, width: 2)),
      ),
    );
  }

  /// 试题数据组件
  _questionTypeItem() {
    if (testType) {
      return StemRichTextWidget(
        text: remark ?? "",
        textStyle: FontUtil.fontBlack1(24),
      );
    } else {
      return Container(
        // margin: EdgeInsets.symmetric(horizontal: 10),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: _showQuestion(content: content),
        ),
      );
    }
  }

  /// 显示试题
  _showQuestion({List<ContentPractice>? content}) {
    List<ContentPractice> txList = content ?? [];
    List<Widget> widgets = [];
    for (int i = 0; i < txList.length; i++) {
      ContentPractice con = txList[i];
      widgets.add(_showQuestionRow(con));

      if (i != txList.length - 1) {
        widgets.add(hiSpace(height: 24));
      }
    }

    return widgets;
  }

  /// 试题类型具体显示
  Widget _showQuestionRow(ContentPractice con) {
    double fraction = con.qs?[0].score ?? 0;
    return Wrap(
      // mainAxisAlignment: MainAxisAlignment.spaceBetween,
      alignment:WrapAlignment.spaceBetween,
      children: [
        Text(
          con.tyName ?? "",
          style: FontUtil.fontBlack1(20),
        ),
        Text(
          "共${con.qs?.length ?? 0}题，$fraction分/题",
          style: FontUtil.fontBlack1(20),
        ),
      ],
    );
  }
}
